const  MDCFile = "./MDCdecks/";
const fs = require("fs");
const bunyan = require('bunyan');
const MDCLOG = bunyan.createLogger({name: "MDCLOG",
    streams:[{
      path: './mdc.log',
    }]
});

//读取指定卡组
const readDeck = async function(deck_name, deck_full_path) {
  const deck={};
  deck.name=deck_name;
  deck_text = await fs.promises.readFile(deck_full_path, { encoding: "ASCII" });
  deck_array = deck_text.split("\n");
  deck.main = [];
  deck.extra = [];
  deck.side = [];
  current_deck = deck.main;
  for (l in deck_array) {
      line = deck_array[l];
      if (line.indexOf("#extra") >= 0) {
          current_deck = deck.extra;
      }
      if (line.indexOf("!side") >= 0) {
          current_deck = deck.side;
      }
      card = parseInt(line);
      if (!isNaN(card)) {
          current_deck.push(card);
      }
  }
  return deck;
}
ygopro.ctos_follow_after("UPDATE_DECK", true, (buffer, info, client, server, datas) => {
    var room = ROOM_all[client.rid];
    if (!room) {
        return null;
    }
    if (room.duel_stage != ygopro.constants.DUEL_STAGE.BEGIN) {
        return null;
    }
    if (client.is_local) {
        return null;
    }

    var room_parameters = room.name.split('#', 2)[0].split(/[,， ]/);
    var found = false;
    for (var parameter of room_parameters) {
        if (parameter.toUpperCase() == "MDC") {
            found = true;
            break;
        }
    }
    if (!found) {
        return null;
    }

    //替换为随机卡组
    var buff_main_new = [];
    var buff_extra_new = [];
    var buff_side_new = [];
    //读取指定卡组
    var readDeck = function (deck_name, deck_full_path) {
      var deck = {};
      deck.name = deck_name;
      deck_text = fs.readFileSync(deck_full_path, { encoding: "ASCII" });
      deck_array = deck_text.split("\n");
      deck.main = [];
      deck.extra = [];
      deck.side = [];
      current_deck = deck.main;
      for (l in deck_array) {
        line = deck_array[l];
        if (line.indexOf("#extra") >= 0) {
          current_deck = deck.extra;
        }
        if (line.indexOf("!side") >= 0) {
          current_deck = deck.side;
        }
        card = parseInt(line);
        if (!isNaN(card)) {
          current_deck.push(card);
        }
      }
      return deck;
    }
    //除了AI都换卡组
    if (client.ip != "::ffff:127.0.0.1") {
      var randomdeckFiles = fs.readdirSync(MDCFile, function (err, files) {
        if (err) {
          return log.warn(err);
        }
        return files
      });

      var randomdeck = randomdeckFiles[Math.floor(Math.random() * (randomdeckFiles.length - 1))]

      var randomdeckdetail = readDeck(randomdeck, MDCFile + randomdeck)

      if (randomdeckdetail.main.length > 0)
        for (var i = 0; i < randomdeckdetail.main.length; i++) {
          buff_main_new.push(randomdeckdetail.main[i])
        }

      if (randomdeckdetail.extra.length > 0)
        for (var i = 0; i < randomdeckdetail.extra.length; i++) {
          buff_main_new.push(randomdeckdetail.extra[i])
        }

      if (randomdeckdetail.side.length > 0)
        for (var i = 0; i < randomdeckdetail.side.length; i++) {
          buff_side_new.push(randomdeckdetail.side[i])
        }

      MDCLOG.info({
        "clientName":client.name,
        "clientDeck":randomdeck
      });
    }

    client.main = buff_main_new.concat(buff_extra_new);
    client.side = buff_side_new;

    var compat_deckbuf = buff_main_new.concat(buff_extra_new).concat(buff_side_new);
    while (compat_deckbuf.length < 90) {
      compat_deckbuf.push(0);
    }

    ygopro.ctos_send(server, "UPDATE_DECK", {
      mainc: client.main.length,
      sidec: client.side.length,
      deckbuf: compat_deckbuf
    });
    return true;
});

ygopro.stoc_follow_after("CHANGE_SIDE", true, (buffer, info, client, server, datas) => {
    var room = ROOM_all[client.rid];
    if (!room) {
        return false;
    }
    ygopro.ctos_send(server, "UPDATE_DECK", {
        mainc: client.main.length,
        sidec: client.side.length,
        deckbuf: client.main.concat(client.side)
    });
    if (client.side_interval) {
        clearInterval(client.side_interval);
    }
    return true;
});

ygopro.stoc_follow_before('DUEL_END', false, async function (buffer, info, client, server, datas) {

   var room = ROOM_all[client.rid];
    if (!room) {
        return null;
    }

   if( room.hostinfo.mode === 2){
      let scores={
        p1:{
          name:room.dueling_players[0].name_vpass,
          score:room.scores[room.dueling_players[0].name_vpass]
        },p2:{
          name:room.dueling_players[1].name_vpass,
          score:room.scores[room.dueling_players[1].name_vpass]
        },p3:{
          name:room.dueling_players[2].name_vpass,
          score:room.scores[room.dueling_players[2].name_vpass]
        },p4:{
          name:room.dueling_players[3].name_vpass,
          score:room.scores[room.dueling_players[3].name_vpass]
        }
      }
    MDCLOG.info(scores);
   }else {
    let scores={
      p1:{
        name:room.dueling_players[0].name_vpass,
        score:room.scores[room.dueling_players[0].name_vpass]
      },p2:{
        name:room.dueling_players[1].name_vpass,
        score:room.scores[room.dueling_players[1].name_vpass]
      }
    }
    MDCLOG.info(scores);
   }
  
}); 